<?php
/** Zend_Controller_Action */ 
require_once ROOT_PATH . '/library/Soooner/Controller/Action.php';
class ConfigController extends Soooner_Controller_Action
{
	private $_configname;
	private $_modelConfig;
	private $_modelScreen;
	private $notiryController;
    public function init()
    {
        /* Initialize action controller here */
    	$this->initDzTemplate();
    	$this->_modelConfig = new Model_Tables_Config();
    	$this->_modelScreen = new Model_Tables_Screen();
    }
    /**
    * 获取配置信息列表
    * @return unknown_type
    */
    public function indexAction()
    {
    	/*$options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
        $ItemCount =  $this->_modelConfig->getCount();
        $PageCount = ceil($ItemCount/$numPerPage);
        $PageCount = $PageCount ? $PageCount : 1;*/
    	$groups = $this->_modelConfig->getGroups();  
		include $this->_dzTemplate->getFile( 'config.manage.html' );
    }
    /**
    * 获取配置信息列表
    * @return unknown_type
    */
    public function indexajaxAction()
    {
        $options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
        $page = intval($this->_getParam('page'));//默认页码
        $page = empty($page)?'1':$page;
   		$s = $this->_getParam('s'); 
        $ItemCount =  $this->_modelConfig->getCount($s);
        $PageCount = ceil($ItemCount/$numPerPage);
        if($page>$PageCount)$page=$PageCount;
        $infos = $this->_modelConfig->getList( $page, $numPerPage, $s );
        include $this->_dzTemplate->getFile( 'ajax/config.manage.ajax.html' );
    }
    /**
    * 添加配置信息
    * @return unknown_type
    */
    public function addAction()
    {
    	$request = $this->getRequest();        
        if ($request->isPost())
        {
            $params =$request->getPost();
            $this->_modelConfig->add($params);                
            $this->_redirect("/config/index/");
        } 
        include $this->_dzTemplate->getFile( 'config.add.html' );
    }
    /**
    * 修改配置信息
    * @return unknown_type
    */
    public function modifyAction()
    {
    	$configname = $this->_getParam('configname');
    	$info = $this->_modelConfig->getInfo($configname); 
        $request = $this->getRequest();  
        $resultinfo = $this->_getParam('resultinfo');      
        if ($request->isPost())
        {
        	$request =$request->getPost();        	
            if (true)//服务端检查输入参数
            {
                $this->_modelConfig->modify($request,$configname);                
                $resultinfo ="修改成功！";
                $this->_redirect("/config/modify/configname/".$configname."/resultinfo/".$resultinfo);
            }
        } 
        include $this->_dzTemplate->getFile( 'config.edit.html' );
    }
    
    /**
    * 无刷新修改配置信息
    * @return unknown_type
    */    
    public function modifyajaxAction()
    {
        $request = $this->getRequest();
        $params = $request->getPost();
        $arr = split("\|", $params['id']);
        $field = $arr[0];
        $configname = $arr[1];
        $value = stripslashes($params['value']);
        $this->_modelConfig->modifyAjax($configname,$field,$value);
        //修改配置信息后刷新屏幕
        $this->_modelScreen->flushAll();
        echo $value;
        exit;
    }
    public function modifyajaxreversAction()
    {
    	$field = $this->_getParam('field');
    	$pri = $this->_getParam('pri');
        $this->_modelConfig->modifyAjaxRevers($field,$pri);
        exit;
    }
    /**
    * 删除配置信息
    */
    public function deleteAction()
    {
        $configname = $this->_getParam('configname');   
        $this->_modelConfig->del($configname); 
        exit;
    }
    
    /**
    * 修改配置信息
    * @return unknown_type
    */
    public function modifystateAction()
    {
    	$configname = $this->_getParam('configname');
    	$info = $this->_modelConfig->getInfo($configname);//主控或受控
    	if($info['configvalue']=="master") {
    		$infop = $this->_modelConfig->getInfo("isproxy");
    		if($infop["configvalue"]){
    			$info['configvalue']="proxymaster";
    		}
    	}
        $request = $this->getRequest();  
        $resultinfo = $this->_getParam('resultinfo');
        $states = array("master"=>"主控","slave"=>"受控","proxymaster"=>"代理");
        if ($request->isPost())
        {
        	$request =$request->getPost();        	
            if (true)//服务端检查输入参数
            {
            	if($request["configvalue"]=="proxymaster"){
            		$request2["configname"]="isproxy";
            		$request2["configvalue"]="1";
            		$this->_modelConfig->modify($request2,$request2["configname"]);
            		$request["configvalue"]="master";
            	}else{
            		$request2["configname"]="isproxy";
            		$request2["configvalue"]="0";
            		$this->_modelConfig->modify($request2,$request2["configname"]);
            	}
            	$this->_modelConfig->modify($request,$configname);
		        if($request["configvalue"]=="master" && function_exists("posix_kill")){
		        	system("wget http://127.0.0.1/notify/touch/signal/35 -O /dev/null");
		    	}
                $resultinfo ="修改成功！";
                echo "<script>top.location.reload();</script>";
            }
        } 
        include $this->_dzTemplate->getFile( 'config.state.edit.html' );
    }
    
    /**
    * 修改配置信息中的发布模式
    * @return unknown_type
    */
    public function modifypubmodAction()
    {
    	$configname = $this->_getParam('configname');
    	if(!$configname)
    		$configname = "pubmod";
    	$info = $this->_modelConfig->getInfo($configname); 
        $request = $this->getRequest();  
        $resultinfo = $this->_getParam('resultinfo');
        $states = array("multy"=>"多机模式","single"=>"单机模式");
        if ($request->isPost())
        {
        	$request =$request->getPost();        	
            if (true)//服务端检查输入参数
            {
            	$this->_modelConfig->modify($request,$configname);
                $resultinfo ="修改成功！";
                $info = $this->_modelConfig->getInfo($configname); 
            }
        } 
        include $this->_dzTemplate->getFile( 'config.pubmod.edit.html' );
    }
}
